package main

import (
	"log"
	"unsafe"
)

import (
	. "github.com/CodyGuo/win"
)

func shutdown() {
	//关机
	var (
		hToken       HANDLE
		tkp          TOKEN_PRIVILEGES
		lpSystemName *byte
		lpName       *byte
	)
	//tmp := []byte("SeSecurityPrivilege")
	//lpName = &tmp[0]
	lpName = StringToBytePtr(SE_SHUTDOWN_NAME)
	lpSystemName = nil
	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)
	LookupPrivilegeValueA(lpSystemName, lpName, &tkp.Privileges[0].Luid)
	tkp.PrivilegeCount = 1 // one privilege to set
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED
	//AdjustTokenPrivileges(hToken, false, &tkp, 0, &tkp, &ReturnLength)
	AdjustTokenPrivileges(hToken, false, &tkp, unsafe.Sizeof(tkp), nil, nil)
	ret2 := GetLastError()
	if ret2 != ERROR_SUCCESS {
		log.Println("提升进程权限失败！")
		return
	} else {
		log.Println("提升进程权限成功！")
		ret := ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0x00000001)
		ret2 := GetLastError()
		if ret == false {
			log.Println("关机失败！错误代码：", ret2)
			return
		} else {
			log.Println("关机执行成功！")
			return
		}

	}
}
